package com.sip.stream.utils.zfy;

import android.content.Context;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.util.Size;

import com.sip.stream.ClientApp;
import com.sip.stream.R;
import com.sip.stream.gbstack.TypeDefine;
import com.sip.stream.utils.GBUtils;
import com.sip.stream.utils.zfy.camera.AbstractDeviceMediaStrategy;
import com.sip.stream.utils.zfy.config.ZFYMediaConfig;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public abstract class ZFYPlatformCommonSettingViewModel {
    private WeakReference<Context> contextWeakReference;
    private ZFYMediaConfig mediaConfig;

    public abstract List<GSConfigBean> getCommonSetting();

    public abstract boolean handlerDataChange(GSConfigBean gSConfigBean);

    public ZFYPlatformCommonSettingViewModel(ZFYMediaConfig zFYMediaConfig, Context context) {
        this.contextWeakReference = new WeakReference<>(context);
        this.mediaConfig = zFYMediaConfig;
    }

    public ZFYMediaConfig getConfig() {
        return this.mediaConfig;
    }

    public Context getContext() {
        return this.contextWeakReference.get();
    }

    public GSConfigBean getDetailSetting() {
        GSConfigBean gSConfigBean = new GSConfigBean(ZFYConfig.PLATFORM_DETAIL_SETTING);
        gSConfigBean.setType("action");
        gSConfigBean.setName(getString(R.string.setting_platform_detailsetting));
        return gSConfigBean;
    }

    public GSConfigBean getDualStream(boolean z) {
        GSConfigBean gSConfigBean = new GSConfigBean(ZFYConfig.PLATFORM_DUAL_STREAM);
        gSConfigBean.setType("bool");
        gSConfigBean.setName(getString(R.string.setting_platform_dualstream));
        gSConfigBean.setValue(z ? "true" : "false");
        return gSConfigBean;
    }

    public GSConfigBean getDynamicRemoteStream(boolean z) {
        GSConfigBean gSConfigBean = new GSConfigBean(ZFYConfig.PLATFORM_DYNAMIC_REMOTE_STREAM);
        gSConfigBean.setType("bool");
        gSConfigBean.setName(getString(R.string.setting_platform_dynamic_remote_stream));
        gSConfigBean.setValue(z ? "true" : "false");
        return gSConfigBean;
    }

    public GSConfigBean getAutoVideo(boolean z) {
        return new GSConfigBean.Builder(ZFYConfig.PLATFORM_AUTORECORDIN_WHEN_REMOTE).setName(getString(R.string.setting_platform_autovideowhenremote)).setType("bool").setBooleanValue(z).build();
    }

    public GSConfigBean getCodeFrameRate(ZFYMediaConfig zFYMediaConfig) {
        GSConfigBean gSConfigBean = new GSConfigBean(ZFYConfig.PLATFORM_STREAM_FPS);
        gSConfigBean.setType("selector");
        gSConfigBean.setName(getString(R.string.setting_platform_streamfps));
        String[] strArr = {"15"};
        if (zFYMediaConfig.getConfigLevel() < 1) {
            strArr = new String[]{TypeDefine.ALARM_METHOD_VIDEO, "10", "15", "25", "30"};
        }
        gSConfigBean.setValue("" + zFYMediaConfig.getStreamFPS());
        gSConfigBean.setSelections(strArr);
        return gSConfigBean;
    }

    public GSConfigBean getStreamPPI(int i, int i2) {
        GSConfigBean gSConfigBean = new GSConfigBean(ZFYConfig.PLATFORM_STREAM_PPI);
        gSConfigBean.setType("selector");
        gSConfigBean.setValue(i + ZFYMediaUtil.SEP_PPI + i2);
        gSConfigBean.setName(getString(R.string.setting_platform_streamppi));
        gSConfigBean.setSelections(getSupportSize());
        return gSConfigBean;
    }

    public GSConfigBean getVideoMinetypeBean(String str) {
        GSConfigBean gSConfigBean = new GSConfigBean();
        gSConfigBean.setType("selector");
        gSConfigBean.setKey(ZFYConfig.PLATFORM_STREAN_MINETYPE);
        gSConfigBean.setName(getString(R.string.setting_platform_streamcodetype));
        String[] videoMinetype = GBUtils.getVideoMinetype();
        for (int i = 0; i < videoMinetype.length; i++) {
            if (videoMinetype[i].contains("HEVC")) {
                videoMinetype[i] = getString(R.string.setting_platform_media_hevc);
            }
        }
        if (videoMinetype.length > 1) {
            gSConfigBean.setType("selector");
        } else {
            gSConfigBean.setType("text");
        }
        if (str.equalsIgnoreCase("video/hevc")) {
            gSConfigBean.setValue(getString(R.string.setting_platform_media_hevc));
        } else {
            gSConfigBean.setValue("H.264/AVC");
        }
        gSConfigBean.setSelections(videoMinetype);
        return gSConfigBean;
    }

    public GSConfigBean getCoordinateSystem(String str) {
        GSConfigBean gSConfigBean = new GSConfigBean(ZFYConfig.PLATFORM_COORDINATE_SYSTEM);
        gSConfigBean.setType("selector");
        gSConfigBean.setName(getString(R.string.setting_platform_coordinatesystem));
        gSConfigBean.setValue("WGS84");
        gSConfigBean.setSelections(new String[]{"WGS84"});
        return gSConfigBean;
    }

    public GSConfigBean getUserBindingAction() {
        GSConfigBean gSConfigBean = new GSConfigBean("setting_platform_user_binding");
        gSConfigBean.setType("action");
        gSConfigBean.setName(getString(R.string.setting_platform_user_binding));
        return gSConfigBean;
    }

    public String[] getSupportSize() {
        int[] pPISelector = GBUtils.getPPISelector();
        if (pPISelector == null || pPISelector.length < 1) {
            return getSupportSizeCamera2();
        }
        ArrayList arrayList = new ArrayList();
        for (int i : pPISelector) {
            if (i == 480) {
                arrayList.add("864X" + i);
            } else {
                arrayList.add("" + ((i / 9) * 16) + ZFYMediaUtil.SEP_PPI + i);
            }
        }
        return (String[]) arrayList.toArray(new String[arrayList.size()]);
    }

    public GSConfigBean getStreamBitrate(int i) {
        GSConfigBean gSConfigBean = new GSConfigBean(ZFYConfig.PLATFORM_STREAM_BITRATE);
        gSConfigBean.setType("selector");
        gSConfigBean.setName(getString(R.string.setting_platform_streambitrate));
        gSConfigBean.setValue("" + i);
        gSConfigBean.setSelections(new String[]{"512", "1024", "2048", "4096"});
        return gSConfigBean;
    }

    public GSConfigBean getConnectstate() {
        GSConfigBean gSConfigBean = new GSConfigBean(ZFYConfig.PLATFORM_STATE_INFO);
        gSConfigBean.setType("action");
        gSConfigBean.setName(getString(R.string.setting_platform_connectstate));
        return gSConfigBean;
    }

    public GSConfigBean getFrontCameraStream(boolean status) {
        return new GSConfigBean.Builder(ZFYConfig.PLATFORM_FRONT_CAMERA).setType("bool").setName(getContext().getString(R.string.setting_platform_frontcamera)).setBooleanValue(status).build();
    }

    public String getString(int resId) {
        return ClientApp.clientApp.getString(resId);
    }

    /***
     * 获取相机分辨率
     * @return
     */
    public static String[] getSupportSizeCamera2() {
        CameraManager cameraManager = (CameraManager) ClientApp.clientApp.getSystemService(Context.CAMERA_SERVICE);
        try {
            String camera2Id = getCamera2Id(false, false, false, cameraManager);
            if (camera2Id == null) {
                camera2Id = cameraManager.getCameraIdList()[0];
            }
            Size[] outputSizes = ((StreamConfigurationMap) cameraManager.getCameraCharacteristics(camera2Id).get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)).getOutputSizes(35);
            ArrayList arrayList = new ArrayList();
            for (Size size : outputSizes) {
                if (size.getHeight() >= 360 && ((int) ((size.getHeight() / 9.0f) * 16.0f)) == size.getWidth()) {
                    arrayList.add("" + size.getWidth() + ZFYMediaUtil.SEP_PPI + size.getHeight());
                }
            }
            return (String[]) arrayList.toArray(new String[arrayList.size()]);
        } catch (CameraAccessException e) {
            e.printStackTrace();
            return new String[0];
        }
    }

    public static String getCamera2Id(boolean frontSide, boolean isInfraredLightOpen, boolean isQRScan, CameraManager cameraManager) {
        try {
            String[] cameraIdList = cameraManager.getCameraIdList();
            if (cameraIdList.length == 4) {
                if (frontSide) {
                    return cameraIdList[1];
                }
                if (isInfraredLightOpen) {
                    return cameraIdList[3];
                }
                if (isQRScan) {
                    return cameraIdList[0];
                }
                return cameraIdList[2];
            }
            return null;
        } catch (CameraAccessException e) {
            e.printStackTrace();
            return null;
        }
    }
}
